Ext.ns('ru.proburo.oms.catalog');

/**
 * ru.proburo.oms.catalog.Activator
 * @extends ru.proburo.oms.module.AbstractActivator
 */
ru.proburo.oms.catalog.Activator = Ext.extend(ru.proburo.oms.module.AbstractActivator, {
    
    start: function(viewport)
    {
        var tree = this._initTree(viewport.layout.west.panel);
        
		viewport.catTree = tree;
		
        this.panel = viewport.layout.center.panel;
        
        this._initTab(tree);
    },
    
    // private
    _initTree: function(panel)
    {
        var tree = panel.add(new ru.proburo.oms.catalog.components.Tree({
            title:   'Каталог',
            iconCls: 'silk-folder'
        }));
        
        panel.doLayout();
        
        return tree;
    },
    
    // private
    _initTab: function(tree)
    {        
        var list = new ru.proburo.oms.catalog.components.List({
            tree: 	   tree,
            activator: this,
			
            region: 'center'
        });
        
        var detail = new ru.proburo.oms.catalog.components.Detail({
            list: list,
			
            header: false,            
            
            region:       'south',
            height:       363,
            
            split:        true,
            collapsible:  true,
			collapseMode: 'mini',
            collapsed:    true
        });
        
        var tab = this.panel.add({
            title:       'Каталог',
            iconCls:     'silk-folder',
            border:      false,
            header:      false,
            forceLayout: true,
            
            layout: 'border',
            
            items:
            [
                list,
                detail
            ]
        });
		this.panel.hideTabStripItem(tab);
		this.panel.catalogTab = tab;
		
        tree.on('expand', function(item){
            var key = this.panel.items.getKey(tab);
            this.panel.hideTabStripItem(this.panel.currentFTab);
            this.panel.setActiveTab(key);
            this.panel.unhideTabStripItem(tab);
            this.panel.currentFTab = tab;
        }, this);
        
        list.on({
            titlechange: function(panel, the){
                tab.setTitle('Каталог: ' + the);
            },
            rowdblclick: function(grid, rowIndex, e){
                detail.expand();
            },
            scope: this
        });
		
		
		var key = this.panel.items.getKey(tab);
		this.panel.hideTabStripItem(this.panel.currentFTab);
		this.panel.setActiveTab(key);
		this.panel.unhideTabStripItem(tab);
		this.panel.currentFTab = tab;
        
        // при первом выделении строки развернуть панель детальной инфы
        /*list.getSelectionModel().on('selectionchange', function(sm){
            detail.expand();
        }, this, {single: true});*/
    }
});
ru.proburo.oms.Bootstrap.reg(ru.proburo.oms.catalog.Activator);